using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Collections.Generic;
using FEL.COM;
using AjaxControlToolkit;

public partial class COM_COM_ARTICULO_ATRIBUTO : PaginaBaseKCG
{


    private string Com_Articulo_Id
    {
        get
        {
            
            if (ViewState["Com_Articulo_Id"] == null)
            {
                ViewState["Com_Articulo_Id"] = ParametroID(Request.QueryString.Get("Com_Articulo_Id"));
            }
            return (string)ViewState["Com_Articulo_Id"];
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
               
        base.Page_Load(sender, e);
        fvcom_articulo_atributo.DataBound += new EventHandler(fvcom_articulo_atributo_DataBound);        
    }

    string nombreart = null;
    string tipoart = null;

    private void DesahabilitarBotones()
    {

        if (Com_Articulo_Id == "ART")
        {
            Button btn2 = ((Button)Fv.FindControl("Button2"));
            btn2.Visible= false;             
        }
        else
            if (Com_Articulo_Id == "SER")
            {
                Button btn1 = ((Button)Fv.FindControl("Button1"));
                btn1.Visible= false;                 
            }          

    }    

    private void DesahabilitarPanel()
    {

        if (Com_Articulo_Id == "ART")
        {
            Panel pnl1 = ((Panel)Fv.FindControl("PanelArticulo"));
            pnl1.GroupingText = GetLocalResourceObject("Rec_Com_Articulo_Atributo_ArticuloRecursoKCG.GroupingText").ToString();
        }
        else
            if (Com_Articulo_Id == "SER")
            {
                Panel pnl1 = ((Panel)Fv.FindControl("PanelArticulo"));
                pnl1.GroupingText = GetLocalResourceObject("Rec_Com_Articulo_Atributo_ServicioRecursoKCG.GroupingText").ToString();
            }

    }   


    protected override void DefinirParametrosFijosDeInsercion(System.Collections.Generic.List<DictionaryEntry> param)
    {
        if (Request.QueryString.Count > 0)
        {
            param.Add(new DictionaryEntry("Com_Articulo_Id", Request.QueryString["Com_Articulo_Id"]));
            param.Add(new DictionaryEntry("Com_Articulo_Codigo", Request.QueryString["Com_Articulo_Codigo"]));
            param.Add(new DictionaryEntry("Com_Articulo_Nombre", Request.QueryString["Com_Articulo_Nombre"]));


        }
        base.DefinirParametrosFijosDeInsercion(param);
    }

    private string ParametroID(string ArticuloId)
    {
        Scope s = (Scope)HttpContext.Current.Session["Scope"];
        FEL.COM.BO_Com_Articulo boComArticulo = new BO_Com_Articulo();

        List<Com_Articulo> litComArticulo = boComArticulo.GetById("", s, ArticuloId);

        return litComArticulo[0].Tipo;
    }

    void fvcom_articulo_atributo_DataBound(object sender, EventArgs e)
    {
        DesahabilitarBotones();
        DesahabilitarPanel();
        if (Fv.CurrentMode == FormViewMode.Insert)
        {
            if (Com_Articulo_Id == "ART")
            {
                ((AutoCompleteExtender)Fv.FindControl("AutoCompleteExtender1")).ServiceMethod = "GetArticulo";
            }
            else
                if (Com_Articulo_Id == "SER")
                {
                    ((AutoCompleteExtender)Fv.FindControl("AutoCompleteExtender1")).ServiceMethod = "GetServicio";
                }
        }

        if (Fv.CurrentMode == FormViewMode.Edit)
        {
            if (Com_Articulo_Id == "ART")
            {
                ((AutoCompleteExtender)Fv.FindControl("AutoCompleteExtender1")).ServiceMethod = "GetArticulo";
            }
            else
                if (Com_Articulo_Id == "SER")
                {
                    ((AutoCompleteExtender)Fv.FindControl("AutoCompleteExtender1")).ServiceMethod = "GetServicio";
                }
        }        
    }

    protected override CheckBox CbInsertarMemoria
    {
        get { return null; }
    }

    protected override Type TipoObjeto
    {
        get { return typeof(global::Com_Atributo); }
    }

    protected override GridView Gv
    {
        get { return gvcom_articulo_atributo; }
    }

    protected override FormView Fv
    {
        get { return fvcom_articulo_atributo; }
    }

    protected override ObjectDataSource OdsGv
    {
        get { return odsgvcom_articulo_atributo; }
    }

    protected override ObjectDataSource OdsDv
    {
        get { return odsfvcom_articulo_atributo; }
    }

    protected override Button BResponderZoom
    {
        get { return Seleccionar; }
    }

    protected override Button BRegreso
    {
        get { return Atras; }
    }

    protected override Koala.KoalaWebControls.KFiltro KftFiltro
    {
        get { return kftFiltro; }
    }
    protected override string Contenedor
    {
        get { return "COM_ARTICULO_ATRIBUTO"; }
    }

    protected void nav_Siguiente(object sender, EventArgs e)
    {
        Gv_SelectedIndexChanged(sender, e);
    }

    [WebMethod]
    static public string[] GetArticulo(string prefixText, int count)
    {
        if (count == 0) count = 10;

        Scope s = (Scope)HttpContext.Current.Session["Scope"];

        FEL.COM.BO_Com_Atributo boComAtributo = new BO_Com_Atributo();
        
        List<Com_Atributo> litComAtributo = boComAtributo.GetByLikeNombreArticulos("Nombre", s, prefixText + "%");

            List<string> opciones = new List<string>();
            for (int i = 0; i < litComAtributo.Count && i < count; i++)
            {
                string value = litComAtributo[i].Id + "||" +
                               litComAtributo[i].Codigo;

                opciones.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(litComAtributo[i].Nombre, value));
            }

            return opciones.ToArray();
        }      
    
    [WebMethod]
    static public string[] GetServicio(string prefixText, int count)
    {
        if (count == 0) count = 10;

        Scope s = (Scope)HttpContext.Current.Session["Scope"];

        FEL.COM.BO_Com_Atributo boComAtributo = new BO_Com_Atributo();
        

        List<Com_Atributo> litComAtributo = boComAtributo.GetByLikeNombreServicios("Nombre", s, prefixText + "%");

        List<string> opciones1 = new List<string>();
        for (int i = 0; i < litComAtributo.Count && i < count; i++)
        {
            string value = litComAtributo[i].Id + "||" +
                           litComAtributo[i].Codigo;

            opciones1.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(litComAtributo[i].Nombre, value));
        }

        return opciones1.ToArray();
    }
    [WebMethod]
    static public string[] GetAtributoDet(string prefixText, int count, string contextKey)
    {
        if (count == 0) count = 10;

      Scope s = (Scope)HttpContext.Current.Session["Scope"];

        FEL.COM.BO_Com_Atributo_Det boComAtributoDet = new BO_Com_Atributo_Det();


        List<Com_Atributo_Det> litComAtributoDet = boComAtributoDet.GetByLikeNombre("Nombre", s, prefixText + "%", contextKey);

        List<string> opciones2 = new List<string>();

        if (!string.IsNullOrEmpty(contextKey))
        {
            for (int i = 0; i < litComAtributoDet.Count && i < count; i++)
            {
                string value = litComAtributoDet[i].Id;

                opciones2.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(litComAtributoDet[i].Nombre, value));
            }
        }

        return opciones2.ToArray();
    }

    private const string comandFormat = "RutaDestino: ~/COM/COM_ATRIBUTO_DET.aspx?Com_Atributo_Id = {0}; Filtro: Nombre; Obtener: Nombre|Nombre";

    protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox Com_AtributoId = (TextBox)Fv.FindControl("Com_Atributo_Id");              
        Button botonZooom = (Button)sender;
        botonZooom.CommandArgument = string.Format(comandFormat, Com_AtributoId.Text);
    }

    protected void Btn_Articulo_Click(object sender, EventArgs e)
    {
        TextBox ComArticuloId = (TextBox)Fv.FindControl("Com_Articulo_Id");
        Response.Redirect("~/COM/COM_ARTICULO.aspx?Com_Articulo_Id=" + ComArticuloId.Text);
    }
}
   
